package com.shop.service.impl;

import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.request.AlipayTradePagePayRequest;
import com.alipay.api.response.AlipayTradePagePayResponse;
import com.shop.config.AlipayConfig;
import com.shop.service.AlipayService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Slf4j
@Service
public class AlipayServiceImpl implements AlipayService {

    @Autowired
    private AlipayClient alipayClient;

    @Override
    public String createPayment(String orderNo, Double totalAmount, String subject) {
        try {
            // 创建支付请求
            AlipayTradePagePayRequest request = new AlipayTradePagePayRequest();
            request.setNotifyUrl(AlipayConfig.NOTIFY_URL); // 异步通知地址
            request.setReturnUrl(AlipayConfig.RETURN_URL); // 同步返回地址

            // 构建业务参数
            String bizContent = "{"
                    + "\"out_trade_no\":\"" + orderNo + "\","
                    + "\"total_amount\":\"" + totalAmount + "\","
                    + "\"subject\":\"" + subject + "\","
                    + "\"product_code\":\"FAST_INSTANT_TRADE_PAY\""
                    + "}";
            request.setBizContent(bizContent);
            // 发起支付请求
             return alipayClient.pageExecute(request,"GET").getBody(); // 返回支付表单的 HTML
        } catch (Exception e) {
            e.printStackTrace();
            return "支付失败，请稍后再试";
        }
    }
}